<?php 
	class Task_type_model extends CI_Model{

		function __construct(){
			parent::__construct();
		}

		function get_task_type_list($limit = 0, $offset = 0, $condtions = array()){
			$where = '1=1';
			if(!empty($condtions['name'])){
				$where .= " AND name like '%".$data['name']."%'";
			}
			$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}task_types  WHERE $where ORDER BY sort desc ";
			if($limit > 0 || $offset > 0){
				$sql .= " LIMIT $limit, $offset";
			}
			$result = $this->db->query($sql)->result_array();
			if($limit > 0 || $offset > 0){
				$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
				return array("count" => $row_num['row_num'], "result" => $result);
			}else{
				return $result;
			}
		}


		function get_type_all(){
			$type_arr = array();

			$sql = "SELECT *,(SELECT p.name from tiku_task_types p where p.id = tiku_task_types.pid) as parent_name FROM tiku_task_types WHERE pid = 0  ORDER BY sort desc ";
			$result = $this->db->query($sql)->result_array();
			foreach ($result as $key => $value) {
				$type_arr[] = $value;
				$childs = $this->get_type_by_pid($value['id']);
				foreach ($childs as $key => $value) {
					$type_arr[] = $value;
				}
			}
			return $type_arr;
		}

		function get_type_by_pid($pid){
			$sql = "SELECT *,(SELECT p.name from tiku_task_types p where p.id = tiku_task_types.pid) as parent_name FROM tiku_task_types WHERE pid = $pid  ORDER BY sort desc ";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}
	}